习题 11: 提问
视频锁定
{$ currentTime | date:'mm:ss' $}
{$ timeLeft | date:'mm:ss' $}
我已经出过很多有关于印出的习题,让你习惯写出简单的东西,但简单的东西都有点无聊,我们现在要做的的事是把资料(data)读到你的程序裡面去。这对你可能会有点难度,你可能一下子搞不明白,不过相信我,无论如何先把习题做了再说。只要做几道练习你就明白了。
一般软件做的事情主要就是下面几件:
- 接受人的输入。
- 改变输入值。
- 印出改变了的值。
到目前为止你只做了印出,但还不会接受或修改人的输入。你还不知道「输入(input)」是什麽意思。閒话少说,我们还是开始做点习题看你能不能明白,下一道习题裡面我们将会有更详细的解释。
print "How old are you? "
age = gets.chomp()
print "How tall are you? "
height = gets.chomp()
print "How much do you weigh? "
weight = gets.chomp()
puts "So, you're #{age} old, #{height} tall and #{weight} heavy."
Note: 注意到我们是使用
puts
吗?puts
会自动产生新行。
你应该看到的结果
$ ruby ex11.rb How old are you? 35 How tall are you? 6’2” How much do you weigh? 180lbs So, you’re ‘35’ old, ‘6'2”’ tall and ‘180lbs’ heavy. $
加分习题
- 上网搜寻一下 Ruby 的
gets
和chomp
的功能是什麽? - 你能找到
gets.chomp
别的用法吗?测试一下你上网找到的例子。 - 用类似的格式再写一段,把问题改成你自己的问题。
在线练习
{$ activeFileHint $}